<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <!-- 搜索结果列表 -->
    <div>
      <!-- 列表加载更多 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <article-item
          v-for="obj in searchResultList"
          :key="obj.art_id"
          :artObj="obj"
          :isShow="false"
          @click="articleClickFn(obj.art_id)"
        ></article-item>
      </van-list>
    </div>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem'
import { searchResultAPI } from '@/api'
export default {
  data () {
    return {
      searchResultList: [], // 搜索文章结果列表
      loading: false,
      finished: false,
      page: 1 // 当前页码
    }
  },
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  created () {
    this.getListFn()
  },
  methods: {
    // 触底加载更多
    onLoad () {
      this.page++
      this.getListFn()
    },
    // 请求文章列表
    async getListFn () {
      const res = await searchResultAPI({
      // 获取路由路径上的值
        keyWord: this.$route.params.keywords,
        page: this.page
      })
      if (res.data.data.results === null || res.data.data.results.length === 0) {
        this.finished = true
        // 全部加载完成后显示在底部,不再触发 load事件
      }
      console.log(res)
      this.searchResultList = [...this.searchResultList, ...res.data.data.results]
      this.loading = false
    },
    // 点击跳转到文章详情页面
    articleClickFn (artId) {
      this.$router.push({
        path: '/art_detail',
        query: {
          aid: artId
        }

      })
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
